package com.smag.system.service.impl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.smag.common.core.domain.AjaxResult;
import com.smag.common.core.domain.entity.FarmCart;
import com.smag.common.core.domain.entity.FarmProduct;
import com.smag.system.domain.dto.UpdateShoppingCartDto;
import com.smag.system.domain.vo.ShoppingCartVo;
import com.smag.system.mapper.FarmCartMapper;
import com.smag.system.mapper.FarmProductMapper;
import com.smag.system.service.FarmCartService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class FarmCartServiceImpl extends ServiceImpl<FarmCartMapper, FarmCart> implements FarmCartService {

    @Resource
    private FarmCartMapper farmCartMapper;

    @Resource
    private FarmProductMapper farmProductMapper;


    @Override
    public List<ShoppingCartVo> getShoppingCart(Long userId) {
        return farmCartMapper.getShoppingCart(userId);
    }

    /**
     * 获取购物车列表
     *
     * @param shoppingCarts
     * @return
     */
    @Override
    public Map[] getShoppingCartData(List<FarmCart> shoppingCarts) {
        Map[] ShoppingCartData = new Map[shoppingCarts.size()];
        for (int i = 0; i < ShoppingCartData.length; i++) {
            FarmCart shoppingCart = shoppingCarts.get(i);
            FarmProduct product = farmProductMapper.selectById(shoppingCart.getProductId());
            HashMap<String, Object> tempData = new HashMap<>();
            tempData.put("id", shoppingCart.getId());
            tempData.put("productID", shoppingCart.getProductId());
            tempData.put("productName", product.getName());
            tempData.put("productImg", product.getAvatar());
            tempData.put("price", product.getPrice());
            tempData.put("num", shoppingCart.getAmount());
            tempData.put("maxNum", Math.floor(product.getAmount() / 2.0));
            tempData.put("check", true);
            ShoppingCartData[i] = tempData;
        }
        return ShoppingCartData;
    }

    @Override
    public FarmCart findShoppingCart(Long userId, Long productId) {
        return farmCartMapper.findShoppingCart(userId, productId);
    }

    @Override
    public AjaxResult updateShoppingCart(UpdateShoppingCartDto updateShoppingCartDto) {
        if (farmCartMapper.updateShoppingCart(updateShoppingCartDto.getUserId(), updateShoppingCartDto.getProductId(), updateShoppingCartDto.getAmount()) > 0) {
            return AjaxResult.success();
        }
        return AjaxResult.error();

    }

    @Override
    public AjaxResult deleteShoppingCart(Long user_id, Long product_id) {
        if (farmCartMapper.deleteShoppingCart(user_id, product_id) > 0) {
            return AjaxResult.success();
        }
        return AjaxResult.error();

    }

    @Override
    public AjaxResult addShoppingCart(FarmCart farmCart) {
        FarmCart oldFarmCart = findShoppingCart(farmCart.getUserId(), farmCart.getProductId());
        if (ObjectUtils.isNotEmpty(oldFarmCart)) {
            oldFarmCart.setAmount(farmCart.getAmount() + oldFarmCart.getAmount());
            updateById(oldFarmCart);
        }else {
            save(farmCart);
        }
        return AjaxResult.success();
    }


}
